/**********************************************************************
 Copyright (C) 2009 Jaakko Vuori

 This file is part of FlexiWorker.
 For more information, see <http://code.google.com/p/flexi-worker/>

 FlexiWorker is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 FlexiWorker is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with FlexiWorker.  If not, see <http://www.gnu.org/licenses/>.
 **********************************************************************/

#include "logparser.h"
#include <QFile>
#include <QRegExp>
#include <QTime>
#include <QLocale>
#include <QDebug>

LogParser::LogParser(LogParserHandler& handler) :
        m_handler(handler)
{
}

void LogParser::parse(const QString& fileName)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        return;
    }

    QString event;
    QDateTime time;
    while (!file.atEnd())
    {
        QString line = QString(file.readLine());

        QRegExp rx("(\\w+): (.+)\n");
        int pos = rx.indexIn(line);
        if (pos > -1)
        {
            event = rx.cap(1);
            time = QDateTime::fromString(rx.cap(2), Qt::ISODate);
            if (m_nextDayChange.isNull())
            {
                if (time.time() < KDayStartTime)
                {
                    m_nextDayChange = QDateTime(time.date(), KDayStartTime);
                }
                else
                {
                    m_nextDayChange = QDateTime(time.date().addDays(1), KDayStartTime);
                }
            }

            if ((event == "START" || event == "BACK"))
            {
                if (m_startTime.isNull())
                {
                    m_startTime = time;
                }
                else
                {
                    if (m_startTime > m_nextDayChange)
                    {
                        m_handler.newDay(m_startTime.date().addDays(0));
                        m_nextDayChange = QDateTime(m_startTime.date().addDays(1), KDayStartTime);
                    }
                    m_handler.newEntry(m_startTime, QDateTime());
                    m_startTime = time;
                }
            }
            else
            {
                if (m_startTime > m_nextDayChange)
                {
                    m_handler.newDay(m_startTime.date().addDays(0));
                    m_nextDayChange = QDateTime(m_startTime.date().addDays(1), KDayStartTime);
                }
                else if (time > m_nextDayChange)
                {
                    m_handler.newEntry(m_startTime, m_nextDayChange);
                    m_handler.newDay(m_startTime.date().addDays(0));
                    m_startTime = m_nextDayChange;
                    m_nextDayChange = QDateTime(m_startTime.date().addDays(1), KDayStartTime);
                }
                m_handler.newEntry(m_startTime, time);
                m_startTime = QDateTime();
                time = QDateTime();
            }
        }
    }
    if (!m_startTime.isNull())
    {
        if (m_startTime > m_nextDayChange)
        {
            m_handler.newDay(m_startTime.date().addDays(0));
        }
        m_handler.newEntry(m_startTime, QDateTime());
    }
    else if (!time.isNull())
    {
        if (time > m_nextDayChange)
        {
            m_handler.newDay(time.date().addDays(0));
        }
        m_handler.newEntry(QDateTime(), time);
    }

    m_nextDayChange = QDateTime(m_nextDayChange.date().addDays(1), KDayStartTime);
    if (QDateTime::currentDateTime() > m_nextDayChange)
    {
        m_handler.newDay(QDate::currentDate());
    }
}
